// pages/addAderss/addAderss.js
var that,
  api = new (require('../../utils/api.js')),
  headimg = api.getimgurl(),
  app = getApp(),
  util = new (require('../../utils/util.js'));
Page({

  /**
   * 页面的初始数据
   */
  data: {
    multiIndex: [0, 0, 0],
    region: [],
    address: "请选择地区",
    addressinfo: '',
    name: '',
    phone: ''
    // customItem: '全部'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this
    let { id } = options
    that.setData({ id },()=>{
      that.getdata()
    })
  },
  getdata(){
    let {id,name, address, addressinfo, phone } = that.data
 
      api.getaddressDetail(id,res=>{
          if(res.data.code==0){
            let detail=res.data.entity.data
            if(detail){
              name=detail.name
              phone=detail.phone
              addressinfo=detail.address
              address=detail.region
            }
            that.setData({name, address, addressinfo, phone})
          }
      })
    
  },
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let { address } = this.data
    address = e.detail.value[0] + e.detail.value[1] + e.detail.value[2]
    this.setData({
      region: e.detail.value,
      address
    })
  },
  inputtap(e) {
    let { value } = e.detail, { prop } = e.currentTarget.dataset
    that.setData({ [prop]: value })
  },
  add() {
    let { name, address, addressinfo, phone ,id } = that.data, err = ''
    if (phone == '' || phone.length < 11) err = '请输入您的电话'
    if (phone.length < 11) err = '请输入正确的电话'
    if (name == '') err = '请输入您的姓名'
    if (addressinfo == '') err = '请输入详细地址'
    if (address == '请选择地区') err = '请选择地区'
    if (err.length > 0) {
      wx.showToast({
        title: err,
        icon: 'none'
      })
      return
    }
    wx.showLoading({
      title: '提交中',
      icon: 'none'
    })
    if(id){
      api.editaddress({ region: address, address: addressinfo, name, phone ,aid:id}, res => {
        wx.hideLoading()
        if (res.data.code == 0) {
          wx.showModal({
            title: '提示',
            content: '修改地址成功',
            showCancel: !1,
            success(res) {
              if (res.confirm) {
                wx.navigateBack()
              }
            }
          })
        }
      })
    }else{

      api.addaddress({ region: address, address: addressinfo, name, phone }, res => {
        wx.hideLoading()
        if (res.data.code == 0) {
          wx.showModal({
            title: '提示',
            content: '添加地址成功',
            showCancel: !1,
            success(res) {
              if (res.confirm) {
                wx.navigateBack()
              }
            }
          })
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})